package io.ganguo.chat.route.server.handler;

import io.ganguo.chat.core.connetion.IMConnection;
import io.ganguo.chat.core.handler.IMHandler;
import io.ganguo.chat.core.protocol.Commands;
import io.ganguo.chat.core.protocol.Handlers;
import io.ganguo.chat.core.transport.Header;
import io.ganguo.chat.core.transport.IMRequest;
import io.ganguo.chat.core.transport.IMResponse;
import io.ganguo.chat.core.util.JacksonMapper;
import io.ganguo.chat.core.util.JsonUtil;
import io.ganguo.chat.route.biz.entity.Message;
import io.ganguo.chat.route.biz.entity.User;
import io.ganguo.chat.route.biz.service.impl.MessageServiceImpl;
import io.ganguo.chat.route.biz.service.impl.UserServiceImpl;
import io.ganguo.chat.route.server.dto.JsonDto;
import io.ganguo.chat.route.server.session.ClientSession;
import io.ganguo.chat.route.server.session.ClientSessionManager;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hasee on 2016-03-28.
 */
@Component
public class OperationHandler extends IMHandler<IMRequest> {
    private Logger logger = LoggerFactory.getLogger(OperationHandler.class);

    @Autowired
    MessageServiceImpl messageService;

    @Autowired
    UserServiceImpl userService;

    @Override
    public short getId() {
        return Handlers.OPERATION;
    }

    @Override
    public void dispatch(IMConnection connection, IMRequest request) {
        Header header = request.getHeader();
        switch (header.getCommandId()) {
            case Commands.GET_FRIENDS://获取好友
                getFriends(connection, request);
                break;
            case Commands.GET_PRESENCE://获取在线
                getPresence(connection, request);
                break;
            case Commands.GET_NEARBY://获取附近的人
                getNearby(connection, request);
                break;
            case Commands.GET_OFFMESSAGE://获取离线消息
                getOffMessage(connection, request);
                break;
            case Commands.GET_NEWS://获取一个人的动态
                getNews(connection, request);
                break;
            default:
                connection.close();
                break;
        }
    }

    private void getNews(IMConnection connection, IMRequest request) {
        //获取动态
    }

    private void getOffMessage(IMConnection connection, IMRequest request) {
        //获取离线消息

        IMResponse resp = new IMResponse();
        Header header = new Header();
        header.setHandlerId(Handlers.OPERATION);
        header.setCommandId(Commands.GET_OFFMESSAGE_SUCCESS);

        List<Message> messageList = messageService.findByToOrderByCreateAtAsc(connection.getUin());

        String jsonStr= JacksonMapper.getJsonFromEntity(messageList);

        resp.setHeader(header);
        resp.writeEntity(new JsonDto(jsonStr));
        connection.sendResponse(resp);

    }

    private void getNearby(IMConnection connection, IMRequest request) {
        //获取附近的人
    }

    private void getPresence(IMConnection connection, IMRequest request) {
        //获取在线的人

        IMResponse resp = new IMResponse();
        Header header = new Header();
        header.setHandlerId(Handlers.OPERATION);
        header.setCommandId(Commands.GET_PRESENCE_SUCCESS);
        Map<Long, ClientSession> sessions = ClientSessionManager.getInstance().sessions();
        ArrayList<Long> uinLong = new ArrayList<Long>();


        List<User>  users=new ArrayList<User>() ;
        for (long uin : sessions.keySet()) {//获取当前除了自己之外的其他登陆者。
            if(connection.getUin()!=uin)
                users.add(
                        userService.findByUin(uin)
                );
        }

        String jsonstr=JacksonMapper.getJsonFromEntity(users);
        resp.setHeader(header);
        resp.writeEntity(new JsonDto(jsonstr));
        connection.sendResponse(resp);
    }

    private void getFriends(IMConnection connection, IMRequest request) {
        //获取好友列表
    }

    public static void main(String[] argc) {

        ArrayList<Integer> integers = new ArrayList<Integer>();

        for (int i = 0; i < 10; i++)
            integers.add(i);

        JSONObject jsonObject = JsonUtil.Array2Json(integers);

        System.out.println(jsonObject);

        Object[] xx = JsonUtil.Json2Array(jsonObject);

        for (int i = 0; i < xx.length; i++) {
            System.out.print(xx[i]);
        }
    }
}
